package cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.service;

import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.entity.SoftwareCopyright;
import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.mapper.SoftwareCopyrightMapper;
import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.result.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 软著服务类
 */
@Service
public class SoftwareCopyrightService {
    @Autowired
    private SoftwareCopyrightMapper softwareCopyrightMapper;


    /**
     * 分页查询软著列表
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param params 查询条件
     * @return 分页结果
     */
    public Result list(int pageNum, int pageSize, Map<String, Object> params) {
        PageHelper.startPage(pageNum, pageSize);
        List<SoftwareCopyright> list = softwareCopyrightMapper.list(params);
        PageInfo<SoftwareCopyright> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    /**
     * 根据ID查询软著
     * @param id 软著ID
     * @return 软著信息
     */
    public Result getById(Integer id) {
        SoftwareCopyright softwareCopyright = softwareCopyrightMapper.getById(id);
        if (softwareCopyright == null) {
            return Result.error("404", "软著不存在");
        }


        return Result.success(softwareCopyright);
    }

    /**
     * 新增软著
     * @param softwareCopyright 软著信息
     * @return 操作结果
     */
    @Transactional
    public Result add(SoftwareCopyright softwareCopyright) {
        softwareCopyrightMapper.add(softwareCopyright);



        return Result.success();
    }

    /**
     * 更新软著
     * @param softwareCopyright 软著信息
     * @return 操作结果
     */
    @Transactional
    public Result update(SoftwareCopyright softwareCopyright) {
        softwareCopyrightMapper.update(softwareCopyright);



        return Result.success();
    }

    /**
     * 删除软著
     * @param id 软著ID
     * @return 操作结果
     */
    @Transactional
    public Result delete(Integer id) {
        // 先删除申报人关联

        // 删除软著
        softwareCopyrightMapper.delete(id);

        return Result.success();
    }
}
